import json
from flask import Flask, render_template, Response
import Report
import Excle
app = Flask(__name__, static_folder='', static_url_path='')
dirParam = "/Volumes/软件/FHCRM/myOffice/static/PDF"
dirParamTemp= "/Volumes/软件/FHCRM/myOffice/DOC"
URLParam = "http://127.0.0.1:5001/static/PDF"
#测试数据:
#http://127.0.0.1:5001/DOs_Details_PDFV3/{'company_name':'丰禾','FHtxt':'OK','QTY':'900'}
#FHType="DOs_Details_PDFV3";
#FHJosn={'company_name':'丰禾','FHtxt':'OK','QTY':'900'}
#FHJosnItem='001'
@app.route('/<FHType>/<FHJosn>')
def sendMessage(FHType,FHJosn):
    #FHType="DOs_Details_PDFV3";
    print(FHType)
    print(FHJosn)
    #dirParam = "/Volumes/软件/FHNODE/CRM/myCRM/PDF"
    #filenameDoc = "DOs_Details_PDFV3.docx"
    #filenamePdf = "DOs_Details_PDFV3.pdf"
    filenameDoc = FHType+".docx"
    filenamePdf = FHType+".pdf"
    contextinfo = FHJosn
    contextdict = eval('('+contextinfo+')');
    #Items= FHJosnItem.split(',')
    #for item in Items:
        #contextdict[item]=contextdict[item].replace("%FH", "\n")

    LibreOffic = Report.FHmain.LibreOfficeFile()
    LibreOffic.docToPdf(contextdict,filenameDoc,dirParam,dirParamTemp)
    #Report.FHMinio.FHmaintry(filenamePdf,dirParam)
    URL =URLParam +"/"+ filenamePdf
    response = Response(json.dumps({'URL':URL}))
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'GET, PUT, OPTIONS'
    ''''response.headers['Content-Disposition']="inline;filename="+filenamePdf
    response.headers['Content-Type']="application/pdf"'''
    response.headers['Access-Control-Allow-Headers'] =\
        'Origin, X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Z-Key'
    return response


@app.route('/xutils/<FHJosn>')
def showXutils(FHJosn):
    myxutils=Excle.fhmain.LibreOfficxlxutils();
    filename=myxutils.action_cfarmc_project_print_xls();
    URL =URLParam +"/"+ filename
    response = Response(json.dumps({'URL':URL}))
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Headers'] =\
        'Origin, X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Z-Key'
    return response

@app.route('/')
def index():
    return "OK";
    #return render_template('index.html')

if __name__ == '__main__':
    #app.run(port=5002)
    myxutils = Excle.fhmain.LibreOfficxlxutils();
    filename = myxutils.action_cfarmc_project_print_xls();